package com.isunland.managesystem.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;

public class AboutUsFragment extends BaseFragment {
  private TextView tv_telnumber;
  private TextView mTvWebSite;
  private String currentVersionName;
  private int currentVersionCode;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.about_us);
    try {
      currentVersionName = MyUtils.getVersionName(getActivity());
      currentVersionCode = MyUtils.getVersionCode(getActivity());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @SuppressLint("StringFormatMatches") @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_about_us, container, false);
    TextView tv_version = (TextView) v.findViewById(R.id.tv_version);
    tv_telnumber = (TextView) v.findViewById(R.id.tv_telnumber);
    mTvWebSite = (TextView) v.findViewById(R.id.tv_website_aboutUs);
    View llPhone = v.findViewById(R.id.ll_phone);
    llPhone.setVisibility(MyUtils.isWeiJia() ? View.GONE : View.VISIBLE);
    mTvWebSite.setText(
        MyUtils.isWeiJia() ? R.string.webSiteContentWeiJia : R.string.webSiteContent);
    mTvWebSite.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View v) {
        // 打开网站.
        try {
          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setData(Uri.parse(mTvWebSite.getText().toString().trim()));
          startActivity(intent);
        } catch (Exception e) {

          e.printStackTrace();
          ToastUtil.show(R.string.noApplicationLinkFound);
        }
      }
    });
    tv_version.setText(getString(R.string.version, currentVersionName, currentVersionCode));
    tv_telnumber.setOnClickListener(new OnClickListener() {
      // 调用系统电话
      @Override public void onClick(View arg0) {
        try {
          Intent dialPhoneIntent = new Intent(Intent.ACTION_DIAL);
          dialPhoneIntent.setData(Uri.parse("tel:" + tv_telnumber.getText()));
          startActivity(dialPhoneIntent);
        } catch (Exception e) {

          e.printStackTrace();
          ToastUtil.show(R.string.noApplicationLinkFound);
        }
      }
    });
    return v;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
